//
// Created by Nasa on 2024/6/2.
//

#include <iostream>
#include <vector>

using namespace std;
struct Node
{
    int value;
    Node *next;
};
//建立链表


Node *build(vector<int> v)
{
    Node *h = new Node{0, nullptr};
    Node *p = h;
    for (int &i : v)
    {
        p->next = new Node{i, nullptr};
        p = p->next;
    }
    p->next = nullptr;
    return h;
}
//打印链表

void printLinkedList(Node *h)
{
    Node *p = h;
    while (p->next != nullptr)
    {
        cout << p->next->value << ' ';
        p = p->next;
    }
    cout << endl;
}
// 查找第一个为n的结点的位置(从0开始), 如果没找到返回-1

int findLinkedList(Node *h, int n)
{
    Node *p=h;
    int i=0;
    while(p->next!= nullptr)
    {
        if(p->next->value==n)
        {
            return i;
        }
        p=p->next;
        i++;
    }
    return -1;
}
// 在第n个节点后插入值为x的结点,如果成功返回1,失败返回0
bool insertLinkedList(Node *h,int n,int x)
{
    if(n<0)
    {
        return 0;
    }
    Node *next=h->next;
    Node *prev=h;
    Node *p;
    int i=0;
    while(prev->next!= nullptr)
    {
        if(i==n)
        {
            p=new Node{x,next->next};
            prev->next->next=p;
            return 1;
        }
        next=next->next;
        prev=prev->next;
        i++;
    }
    return 0;
}

int main()
{
    vector<int> l = {5, 4, 3, 2, 1, 0};
    Node *head = build(l);
    printLinkedList(head);
//    cout << findLinkedList(head, 0) << endl;
//    cout << insertLinkedList(head, 0, 9) << endl;
    printLinkedList(head);
    return 0;
}

